
//function to check/uncheck all the checkboxs
$(document).ready(function(){
	$("#IdAllCheck").click(function(){
		var a=$(this).prop("checked");
		$('[name="IdCheck"]').each(function(){
			$(this).prop("checked",a);
		});
	});
});

//function to check/uncheck the all-checkbox
$(document).ready(function(){
	$('[name="IdCheck"]').each(function(){
		$(this).click(function(){
			var tong=true;
			$('[name="IdCheck"]').each(function(){
				if(this.checked==false) {tong=false;return false;}
			});
			$("#IdAllCheck").prop("checked",tong);	
		});
	});
});

//function to confirm delete
$(document).ready(function(){
	$(".deletebutton").click(function(){
		var tong=false;
		var count=0;
		$('[name="IdCheck"]').each(function(){
			if(this.checked==true) {tong=true;count++;}
		});
		if(tong==true){
			var q=confirm("Warning!! You are about to delete "+count+" line(s) from the database. This action can NOT be undone.Proceed to delete?");
			if(q==true){$(this).parents("form").submit();}
		}
	});
});

//function to display types of search form
function showsearchform(){
		if($('[name="searchtype"]').prop("value")==1){
			$(".manusearchform").css('display',"block");
			$(".productsearchform").css('display',"none");
		}
		else{
			$(".manusearchform").css('display',"none");
			$(".productsearchform").css('display',"block");
		}
}

//call the above function when load page and when change value of selectbox, and also hide all the element have class:extra
$(document).ready(function(){
	showsearchform();
	$(".extra").css('visibility',"hidden");
});
$(document).ready(function(){
$('[name="searchtype"]').change(function(){
		showsearchform();});
});

//function to show the extra textfield for the option between of the oppoptionbox
$(document).ready(function(){
	$(".optionbox").change(function(){
		if($(this).prop("value")=='7'){
			$(this).parents("tr").children(".extra").css('visibility',"visible");
		}else{
			$(this).parents("tr").children(".extra").css('visibility',"hidden");
		}
	});
});

//function to validate manufacturer form
function validateManu(){
	var name=$('[name="name"]').prop("value");
	var address=$('[name="addr"]').prop("value");
	var telephone=$('[name="telephone"]').prop("value");
	var ketqua=true;
	if(name=="" || name== null){$("#mesname").prop("innerHTML","Manufacturer name must be filled out.");ketqua=false;}
								else{$("#mesname").prop("innerHTML","");}
	
	if(address=="" || address==null){$("#mesaddr").prop("innerHTML","Manufacturer address must be filled out.");ketqua=false;}
								else{$("#mesaddr").prop("innerHTML","");}
	if(name.length>=200){$("#mesname").prop("innerHTML","Manufacturer name is too long.");ketqua=false;}
	if(address.length>=200){$("#mesaddr").prop("innerHTML","Manufacturer address is too long.");ketqua=false;}
	var thu=/\D/;
	var so=thu.test(telephone);
	if(so){$("#mestelephone").prop("innerHTML","Invalid telephone number.");ketqua=false;}
	      else{$("#mestelephone").prop("innerHTML","");}
	return ketqua;
}

//trigger validate manufacturer form
$(document).ready(function(){
	$(".submitmanu").click(function(){
		var kq=validateManu();
		if(kq) {$(this).parents("form").submit();}
	});
});


//validate product form
function validateProduct(){
	var name=$('[name="name"]').prop("value");
	var price=$('[name="price"]').prop("value");
	var quantity=$('[name="quantity"]').prop("value");
	var ketqua=true;
	if(name=="" || name==null) {$("#mesnamepr").prop("innerHTML","Product name must be filled out.");ketqua=false;}
	else{$("#mesnamepr").prop("innerHTML","");}
	if(name.length>=200){$("#mesnamepr").prop("innerHTML","Product name is too long.");ketqua=false;}
	
	var thuso=/\D/;
	var kolaso;
	kolaso=thuso.test(price);
	
	if(price=="" || price==null) {$("#mesprice").prop("innerHTML","Price must be filled out.");ketqua=false;}
	else{
		if(kolaso){$("#mesprice").prop("innerHTML","Invalid product price.");ketqua=false;}
		else{
			if(price.length>=200){$("#mesprice").prop("innerHTML","Price is too large");ketqua=false;}
			else{$("#mesprice").prop("innerHTML","");};
		}
	}
	
	kolaso=thuso.test(quantity);
	if(quantity=="" || quantity==null) {$("#mesquantity").prop("innerHTML","Quantity must be filled out.");ketqua=false;}
	else{
		if(kolaso){$("#mesquantity").prop("innerHTML","Invalid product quantity.");ketqua=false;}
		else{
			if(quantity.length>=200){$("#mesquantity").prop("innerHTML","Quantity is too large");ketqua=false;}
			else{$("#mesquantity").prop("innerHTML","");};
		}
	}
	return ketqua;
	
}

//trigger validate product form
$(document).ready(function(){
	$(".submitproduct").click(function(){
		var kq=validateProduct();
		if(kq) {$(this).parents("form").submit();}
	});
});
